package com.zgjkhis.modules.system.service.mapper;

import com.zgjkhis.common.mapper.CoreMapper;
import com.zgjkhis.modules.system.domain.Region;
import com.zgjkhis.modules.system.domain.vo.RegionTree;
import com.zgjkhis.modules.system.domain.vo.RegionVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author yzq
 * @Description 行政
 * @Date 2021/6/23 9:48
 * @Param
 * @return
 **/
@Repository
public interface RegionMapper extends CoreMapper<Region> {


    /**
     * 懒加载列表
     *
     * @param parentCode
     * @param param
     * @return
     */
    @Select("<script> SELECT\n" +
            "            region.*,\n" +
            "            ( SELECT CASE WHEN count( 1 ) > 0 THEN 1 ELSE 0 END FROM his_region WHERE parent_code = region.code ) AS \"has_children\"\n" +
            "        FROM\n" +
            "            his_region region\n" +
            "        <where>\n" +
            "            <if test=\"param1!=null\">\n" +
            "                and region.parent_code = #{param1}\n" +
            "            </if>\n" +
            "            <if test=\"param2.code!=null and param2.code!=''\">\n" +
            "                and region.code like concat(concat('%', #{param2.code}),'%')\n" +
            "            </if>\n" +
            "            <if test=\"param2.name!=null and param2.name!=''\">\n" +
            "                and region.name like concat(concat('%', #{param2.name}),'%')\n" +
            "            </if>\n" +
            "        </where></script>")
    List<RegionVO> lazyList(String parentCode, Map<String, Object> param);

    /**
     * 懒加载列表
     *
     * @param parentCode
     * @param param
     * @return
     */
    @Select("<script> SELECT\n" +
            "            region.code AS \"id\",\n" +
            "            region.parent_code AS \"parent_id\",\n" +
            "            region.name AS \"title\",\n" +
            "            region.code AS \"value\",\n" +
            "            region.code AS \"key\",\n" +
            "            ( SELECT CASE WHEN count( 1 ) > 0 THEN 1 ELSE 0 END FROM his_region WHERE parent_code = region.code ) AS \"has_children\"\n" +
            "        FROM\n" +
            "            his_region region\n" +
            "        <where>\n" +
            "            <if test=\"param1!=null\">\n" +
            "                and region.parent_code = #{param1}\n" +
            "            </if>\n" +
            "            <if test=\"param2.code!=null and param2.code!=''\">\n" +
            "                and region.code like concat(concat('%', #{param2.code}),'%')\n" +
            "            </if>\n" +
            "            <if test=\"param2.name!=null and param2.name!=''\">\n" +
            "                and region.name like concat(concat('%', #{param2.name}),'%')\n" +
            "            </if>\n" +
            "        </where>\n" +
            "        ORDER BY region.code</script>")
    List<RegionTree> lazyTree(String parentCode, Map<String, Object> param);

}
